package com.whp.openapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.whp.openapi.common.ResponseResult;
import com.whp.openapi.common.ResultTable;
import com.whp.openapi.dao.AdminUserMapper;
import com.whp.openapi.entity.AdminUser;
import com.whp.openapi.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author whp
 * @date 2021/11/23
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private AdminUserMapper userMapper;

    @Override
    public AdminUser getUserByEmail(String email) {
        if (StringUtil.isNotEmpty(email)) {
            AdminUser userByEmail = userMapper.getUserByEmail(email);
            if (userByEmail != null) {
                return userByEmail;
            }
        }
        return null;
    }

    @Override
    public AdminUser doLogin(String email, String password) {
        if (StringUtil.isNotEmpty(email) && StringUtil.isNotEmpty(password)) {
            AdminUser user = userMapper.getUserByEmail(email);
            if (user != null) {
                return user;
            }
        }
        return null;
    }

    @Override
    public ResultTable table(Integer page, 
                             Integer limit, 
                             String realName, 
                             String email, 
                             Integer status) {
        PageHelper.startPage(page, limit);
        List<AdminUser> users = userMapper.table(realName, email, status);
        PageInfo pi = new PageInfo(users);
        return new ResultTable(0, "msg", (int) pi.getTotal(), pi.getList());
    }

    @Override
    public List<Integer> getUserRole(Integer userId) {
        return userMapper.getUserRole(userId);
    }

    @Override
    public ResponseResult assignRole(Integer userId, Integer[] roleIds) {
        //1.删除user_role的所有userId的数据
        int i = userMapper.deleteUserRole(userId);
        //2.批量添加roleIds数组中的数据
        int i2 = userMapper.addUserRole(userId, roleIds);
        return ResponseResult.getSuccess();
    }

    @Override
    public ResponseResult delUsers(Integer[] ids) {
        int i = userMapper.delUsers(ids);
        if (i > 0) {
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("删除失败");
    }

    @Override
    public ResponseResult addUser(AdminUser user) {
        if (user != null) {
            int i = userMapper.insertSelective(user);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("请提供信息");
    }

    @Override
    public ResponseResult updateUser(AdminUser user) {
        if (user != null) {
            int i = userMapper.updateByPrimaryKeySelective(user);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("请提供信息");
    }
}
